#include <QStyleOptionGraphicsItem>
#include <QGraphicsView>
#include <QPainter>

#include "UIWindow.h"
#include "UIView.h"

// ===========================================================================
//  PRIVATE Class
// ===========================================================================
class UIViewPrivate {
    public:
        QRectF boundingRect;
};

// ===========================================================================
//  PUBLIC Constructors/Destructor
// ===========================================================================
UIView::UIView (QGraphicsItem *parent)
    : QGraphicsItem(parent), UIResponder(window(), this), d(new UIViewPrivate)
{
}

UIView::~UIView() {
}

// ===========================================================================
//  PUBLIC Properties
// ===========================================================================
qreal UIView::width (void) const {
    return(d->boundingRect.width());
}

qreal UIView::height (void) const {
    return(d->boundingRect.height());
}

QSizeF UIView::size (void) const {
    return(d->boundingRect.size());
}

void UIView::setSize (const QSizeF& size) {
    d->boundingRect = QRectF(QPointF(0.0f, 0.0f), size);
}

void UIView::setSize (qreal width, qreal height) {
    d->boundingRect = QRectF(0.0f, 0.0f, width, height);
}

QRectF UIView::boundingRect (void) const {
    return(d->boundingRect);
}

void UIView::setBoundingRect (const QRectF& boundingRect) {
    d->boundingRect = boundingRect;
    update();
}

void UIView::setBoundingRect (qreal x, qreal y, qreal w, qreal h) {
    d->boundingRect = QRectF(x, y, w, h);
    update();
}

UIWindow *UIView::window (void) const {
    return(static_cast<UIWindow *>(scene()));
}

// ===========================================================================
//  PUBLIC Methods
// ===========================================================================
void UIView::removeChildItems (bool destroy) {
    foreach (QGraphicsItem *item, childItems()) {
        scene()->removeItem(item);
        if (destroy) delete item;
    }
}

// ===========================================================================
//  PROTECTED Methods
// ===========================================================================
void UIView::paint (QPainter *painter, 
                    const QStyleOptionGraphicsItem *option,
                    QWidget *widget)
{
    Q_UNUSED(widget)

    painter->save();
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setRenderHint(QPainter::TextAntialiasing);
    painter->setRenderHint(QPainter::SmoothPixmapTransform);

    drawRect(painter, option->rect);

    painter->restore();
}

void UIView::drawRect (QPainter *painter, const QRect& rect) {
    Q_UNUSED(painter)
    Q_UNUSED(rect)
}

void UIView::setUpdatesEnabled (bool enable) {
    foreach (QGraphicsView *view, scene()->views())
        view->setUpdatesEnabled(enable);
}

// ===========================================================================
//  PROTECTED Methods
// ===========================================================================
void UIView::mouseReleaseEvent (QGraphicsSceneMouseEvent *event) {
    if (handleTouches()) touchReleaseEvent(event);
}

void UIView::mousePressEvent (QGraphicsSceneMouseEvent *event) {
    if (handleTouches()) touchPressEvent(event);
}

void UIView::mouseMoveEvent (QGraphicsSceneMouseEvent *event) {
    if (handleTouches()) touchMoveEvent(event);
}

